<?php
class My_Service_Export{
    public function __construct(){
        error_reporting(E_ERROR | E_WARNING | E_PARSE);
    }
    /*
    $exportData = array(
        'index'=>string
        'result'=>array
        'gameId'=>string
        'widgetDescription'=>object
        'configWidget'=>object
    )
    */
    public function exportChannel($exportData){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            die('Có lỗi trong quá trình xuất file excel. Vui lòng thử lại sau hoặc thông báo cho team tech để khắc phục sự cố!');
        }
        
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet($exportData['index']);
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,12);
        $worksheet1->setColumn(1,30,12);
        
		$title = &$workbook->addFormat();
		$title->setBold();
        $title->setSize(12);
		$title->setColor('white');
		$title->setFgColor('grey');
		$title->setHAlign('center');
		$title->setVAlign('center');
		$title->setBorder(1);	
        $title->setTextWrap();
        
		//end style	
		$header = &$workbook->addFormat();
		$header->setBold();
		$header->setColor('white');
		$header->setFgColor('grey');
		$header->setHAlign('center');
		$header->setVAlign('center');
		$header->setBorder(1);	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat();
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('left');
		$textStyle->setVAlign('center');
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setHAlign('right');
		$numberFormat->setVAlign('center');
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setHAlign('right');
		$numberFormat2->setVAlign('center');
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        
        //data
        $arrDate = array();
        $arrResult = array();
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                foreach($result as $channel=>$resultChannel){
                    if(is_array($resultChannel['result'])){
                        $arrDate = array_merge($arrDate,array_keys($resultChannel['result']));
                        foreach($resultChannel['result'] as $date=>$row){
                            if(isset($arrResult[$gameId][$index][$date][$channel])){
                                $arrResult[$gameId][$index][$date][$channel] += $row;
                            }else{
                                $arrResult[$gameId][$index][$date][$channel] = $row;
                            }
                        }
                    }
                }
            }
        }
        $arrDate = array_unique($arrDate);
        
        //title
        $numRow = 0;
        $numColumn = 0;
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $worksheet1->write($numRow, $numColumn, ((isset($exportData['widgetDescription']->api->$exportData['index'])) ? $exportData['widgetDescription']->api->$exportData['index']->name : $exportData['index']).' '.$exportData['gameId'].' ( '.$exportData['dateStart'].' )', $header);
        else
            $worksheet1->write($numRow, $numColumn, ((isset($exportData['widgetDescription']->api->$exportData['index'])) ? $exportData['widgetDescription']->api->$exportData['index']->name : $exportData['index']).' '.$exportData['gameId'].' ( '.$exportData['dateStart'].' - '.$exportData['dateEnd'].' )', $header);
        $worksheet1->setMerge(0,0,0,4);
        
        // header
        $numRow+=2;
        $numColumn = 0;
        $worksheet1->write($numRow, $numColumn, 'Thời gian', $header);
        $numColumnStart = $numColumn + 1;
        $numRowStart = $numRow;
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                foreach($result as $row){
                    $worksheet1->write($numRow, ++$numColumn, ((isset($exportData['widgetDescription']->api->$index)) ? $exportData['widgetDescription']->api->$index->name : $index), $header);
                }
                $worksheet1->setMerge($numRow,$numColumnStart,$numRow,$numColumn);
            }
        }
        $numColumn = 0;
        $worksheet1->write(++$numRow, $numColumn, 'Thời gian', $header);
        $worksheet1->setMerge($numRowStart,$numColumn,$numRow,$numColumn);
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                foreach($exportData['configWidget']->$gameId->$index->channel as $channel){
                    //echo '<th class="text-center">',$channel,'</th>';
                    $worksheet1->write($numRow, ++$numColumn, $channel, $header);
                }
            }
        }
        
        //data
        foreach($arrDate as $date){
            $numRow++;
            $numColumn=0;
            $worksheet1->write($numRow, $numColumn, date("d/m/Y",strtotime($date)), $textStyle);
            foreach($exportData['result'] as $gameId=>$resultIndex){
                foreach($resultIndex as $index=>$result){
                    foreach($exportData['configWidget']->$gameId->$index->channel as $channel){
                        $number = (isset($arrResult[$gameId][$index][$date][$channel])) ? $arrResult[$gameId][$index][$date][$channel] : 0;
                        if($number){
                            $worksheet1->write($numRow, ++$numColumn, $number, $numberFormat);
                        }else{
                            $worksheet1->write($numRow, ++$numColumn, 0, $numberFormat2);
                        }
                    }
                }
            }
        }
        
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $workbook->send($exportData['index'].'_'.$exportData['gameId'].'_'.$exportData['dateStart'].".xls");
        else
		  $workbook->send($exportData['index'].'_'.$exportData['gameId'].'_'.$exportData['dateStart'].'_'.$exportData['dateEnd'].".xls");
		$workbook->close();
    }
    
    public function exportServer($exportData){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            die('Có lỗi trong quá trình xuất file excel. Vui lòng thử lại sau hoặc thông báo cho technical để khắc phục sự cố!');
        }
        
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet($exportData['index']);
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,12);
        $worksheet1->setColumn(1,30,12);
        
		$title = &$workbook->addFormat();
		$title->setBold();
        $title->setSize(12);
		$title->setColor('white');
		$title->setFgColor('grey');
		$title->setHAlign('center');
		$title->setVAlign('center');
		$title->setBorder(1);	
        $title->setTextWrap();
        
		//end style	
		$header = &$workbook->addFormat();
		$header->setBold();
		$header->setColor('white');
		$header->setFgColor('grey');
		$header->setHAlign('center');
		$header->setVAlign('center');
		$header->setBorder(1);	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat();
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('left');
		$textStyle->setVAlign('center');
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setHAlign('right');
		$numberFormat->setVAlign('center');
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setHAlign('right');
		$numberFormat2->setVAlign('center');
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        
        //data
        $arrDate = array();
        $arrResult = array();
        $arrServer = array();
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                if(is_array($result['result'])){
                    $arrDate = array_merge($arrDate,array_keys($result['result']));
                    foreach($result['result'] as $date=>$value){
                        foreach($value as $row){
                            $arrServer[] = $row['server_id'];
                            if(isset($arrResult[$gameId][$index][$date][$row['server_id']])){
                                $arrResult[$gameId][$index][$date][$row['server_id']] += $row['value'];
                            }else{
                                $arrResult[$gameId][$index][$date][$row['server_id']] = $row['value'];
                            }
                        }
                    }
                }
            }
        }
        $arrDate = array_unique($arrDate);
        $arrServer = array_unique($arrServer);
        
        //title
        $numRow = 0;
        $numColumn = 0;
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $worksheet1->write($numRow, $numColumn, ((isset($exportData['widgetDescription']->api->$exportData['index'])) ? $exportData['widgetDescription']->api->$exportData['index']->name : $exportData['index']).' '.$exportData['gameId'].' ( '.$exportData['dateStart'].' )', $header);
        else
            $worksheet1->write($numRow, $numColumn, ((isset($exportData['widgetDescription']->api->$exportData['index'])) ? $exportData['widgetDescription']->api->$exportData['index']->name : $exportData['index']).' '.$exportData['gameId'].' ( '.$exportData['dateStart'].' - '.$exportData['dateEnd'].' )', $header);
        $worksheet1->setMerge(0,0,0,4);
        
        // header
        $numRow+=2;
        $numColumn = 0;
        $worksheet1->write($numRow, $numColumn, 'Thời gian', $header);
        $numColumnStart = $numColumn + 1;
        $numRowStart = $numRow;
        
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                foreach($exportData['configWidget']->$gameId->$index->server as $row){
                    $worksheet1->write($numRow, ++$numColumn, ((isset($exportData['widgetDescription']->api->$index)) ? $exportData['widgetDescription']->api->$index->name : $index), $header);
                }
                $worksheet1->setMerge($numRow,$numColumnStart,$numRow,$numColumn);
            }
        }
        $numColumn = 0;
        $worksheet1->write(++$numRow, $numColumn, 'Thời gian', $header);
        $worksheet1->setMerge($numRowStart,$numColumn,$numRow,$numColumn);
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                foreach($exportData['configWidget']->$gameId->$index->server as $server){
                    //echo '<th class="text-center">',$channel,'</th>';
                    $worksheet1->write($numRow, ++$numColumn, $server, $header);
                }
            }
        }
        
        foreach($arrDate as $date){
            $numRow++;
            $numColumn=0;
            $worksheet1->write($numRow, $numColumn, date("d/m/Y",strtotime($date)), $textStyle);
            foreach($exportData['result'] as $gameId=>$resultIndex){
                foreach($resultIndex as $index=>$result){
                    foreach($exportData['configWidget']->$gameId->$index->server as $server){
                        $number = (isset($arrResult[$gameId][$index][$date][$server])) ? $arrResult[$gameId][$index][$date][$server] : 0;
                        if($number){
                            $worksheet1->write($numRow, ++$numColumn, $number, $numberFormat);
                        }else{
                            $worksheet1->write($numRow, ++$numColumn, $number, $numberFormat2);
                        }
                    }
                }
            }
        }
        
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $workbook->send($exportData['index'].'_'.$exportData['gameId'].'_'.$exportData['dateStart'].".xls");
        else
		  $workbook->send($exportData['index'].'_'.$exportData['gameId'].'_'.$exportData['dateStart'].'_'.$exportData['dateEnd'].".xls");
		$workbook->close();
    }
    
    public function exportGame($exportData){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            die('Có lỗi trong quá trình xuất file excel. Vui lòng thử lại sau hoặc thông báo cho technical để khắc phục sự cố!');
        }
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet($exportData['index']);
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,12);
        $worksheet1->setColumn(1,1,12);
        
		$title = &$workbook->addFormat();
		$title->setBold();
        $title->setSize(12);
		$title->setColor('white');
		$title->setFgColor('grey');
		$title->setHAlign('center');
		$title->setVAlign('center');
		$title->setBorder(1);	
        $title->setTextWrap();
        
		//end style	
		$header = &$workbook->addFormat();
		$header->setBold();
		$header->setColor('white');
		$header->setFgColor('grey');
		$header->setHAlign('center');
		$header->setVAlign('center');
		$header->setBorder(1);	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat();
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('left');
		$textStyle->setVAlign('center');
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setHAlign('right');
		$numberFormat->setVAlign('center');
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setHAlign('right');
		$numberFormat2->setVAlign('center');
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        
        //data
        $arrDate = array();
        $arrResult = array();
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                $arrDate = array_merge($arrDate,array_keys($result['result']));
                foreach($result['result'] as $key=>$val){
                    $arrResult[$gameId][$index][$key]['ALLGAME'] = $val;
                }
            }
        }
        $arrDate = array_unique($arrDate);
                
        //title
        $numRow = 0;
        $numColumn = 0;
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $worksheet1->write($numRow, $numColumn, ((isset($exportData['widgetDescription']->api->$exportData['index'])) ? $exportData['widgetDescription']->api->$exportData['index']->name : $exportData['index']).' '.$exportData['gameId'].' ( '.$exportData['dateStart'].' )', $header);
        else
            $worksheet1->write($numRow, $numColumn, ((isset($exportData['widgetDescription']->api->$exportData['index'])) ? $exportData['widgetDescription']->api->$exportData['index']->name : $exportData['index']).' '.$exportData['gameId'].' ( '.$exportData['dateStart'].' - '.$exportData['dateEnd'].' )', $header);
        $worksheet1->setMerge(0,0,0,2);
        
        // header
        $numRow+=2;
        $numColumn = 0;
        $worksheet1->write($numRow, $numColumn, 'Thời gian', $header);
        
        $worksheet1->write($numRow, ++$numColumn, ((isset($exportData['widgetDescription']->api->$exportData['index'])) ? $exportData['widgetDescription']->api->$exportData['index']->name : $exportData['index']), $header);
        
        //data
        foreach($exportData['result'] as $gameId=>$resultIndex){
            foreach($resultIndex as $index=>$result){
                if(isset($result['status']) && $result['status'] == 1){
                    foreach($result['result'] as $date=>$number){
                        $numColumn=0;
                        $worksheet1->write(++$numRow, $numColumn, date("d/m/Y",strtotime($date)), $textStyle);
                        if($number){
                            $worksheet1->write($numRow, ++$numColumn, $number, $numberFormat);
                        }else{
                            $worksheet1->write($numRow, ++$numColumn, $number, $numberFormat2);
                        }
                    }
                }
            }
        }
        
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $workbook->send($exportData['index'].'_'.$exportData['gameId'].'_'.$exportData['dateStart'].".xls");
        else
		  $workbook->send($exportData['index'].'_'.$exportData['gameId'].'_'.$exportData['dateStart'].'_'.$exportData['dateEnd'].".xls");
		$workbook->close();
    }
    
    /*
    $exportData = array(
        'index'=>string
        'result'=>array
        'gameId'=>string
        'configAPIGame'=>object
        'configChannelGame'=>object
    )
    */
    public function exportCardTransaction($exportData){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            die('Có lỗi trong quá trình xuất file excel. Vui lòng thử lại sau hoặc thông báo cho technical để khắc phục sự cố!');
        }
        
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet($exportData['index']);
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,6);
        $worksheet1->setColumn(1,2,25);
        $worksheet1->setColumn(3,4,12);
        $worksheet1->setColumn(5,5,25);
        $worksheet1->setColumn(6,30,12);
        
		$title = &$workbook->addFormat();
		$title->setBold();
        $title->setSize(12);
		$title->setColor('white');
		$title->setFgColor('grey');
		$title->setHAlign('center');
		$title->setVAlign('center');
		$title->setBorder(1);	
        $title->setTextWrap();
        
		//end style	
		$header = &$workbook->addFormat();
		$header->setBold();
		$header->setColor('white');
		$header->setFgColor('grey');
		$header->setHAlign('center');
		$header->setVAlign('center');
		$header->setBorder(1);	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat();
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('left');
		$textStyle->setVAlign('center');
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setAlign('vcentre');
		$numberFormat->setHAlign('right');
		$numberFormat->setVAlign('center');
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setHAlign('right');
		$numberFormat2->setVAlign('center');
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        
        //title
        $numRow = 0;
        $numColumn = 0;
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $worksheet1->write($numRow, $numColumn, 'Card transaction '.$exportData['gameId'].' ( '.$exportData['dateStart'].' )', $header);
        else
            $worksheet1->write($numRow, $numColumn, 'Card transaction '.$exportData['gameId'].' ( '.$exportData['dateStart'].' - '.$exportData['dateEnd'].' )', $header);
        $worksheet1->setMerge(0,0,0,4);
        
        // header
        $numRow+=2;
        $numColumn = 0;
        
        $name_trans_ID = 'Transaction ID';
        if(isset($exportData['configAPIGame']->card->allow->view->trans_ID)){
            $name_trans_ID = $exportData['configAPIGame']->card->allow->view->trans_ID;
        }
        $name_secondary_agent_id = 'Channel Secondary';
        if(isset($exportData['configAPIGame']->card->allow->view->secondary_agent_id)){
            $name_secondary_agent_id = $exportData['configAPIGame']->card->allow->view->secondary_agent_id;
        }
        $name_username = 'Username';
        if(isset($exportData['configAPIGame']->card->allow->view->username)){
            $name_username = $exportData['configAPIGame']->card->allow->view->username;
        }
        $name_amount = 'Tiền';
        if(isset($exportData['configAPIGame']->card->allow->view->amount)){
            $name_amount = $exportData['configAPIGame']->card->allow->view->amount;
        }
        $name_card_type = 'Loại thẻ';
        if(isset($exportData['configAPIGame']->card->allow->view->card_type)){
            $name_card_type = $exportData['configAPIGame']->card->allow->view->card_type;
        }
        $name_card_pin = 'PIN';
        if(isset($exportData['configAPIGame']->card->allow->view->card_pin)){
            $name_card_pin = $exportData['configAPIGame']->card->allow->view->card_pin;
        }
        $name_card_serial = 'Serial';
        if(isset($exportData['configAPIGame']->card->allow->view->card_serial)){
            $name_card_serial = $exportData['configAPIGame']->card->allow->view->card_serial;
        }
        $name_date = 'Thời gian';
        if(isset($exportData['configAPIGame']->card->allow->view->date)){
            $name_date = $exportData['configAPIGame']->card->allow->view->date;
        }
        $name_date = 'Trạng thái';
        if(isset($exportData['configAPIGame']->card->allow->view->status)){
            $name_status = $exportData['configAPIGame']->card->allow->view->status;
        }
        $name_date = 'Ghi chú';
        if(isset($exportData['configAPIGame']->card->allow->view->note)){
            $name_note = $exportData['configAPIGame']->card->allow->view->note;
        }
        
        $worksheet1->write($numRow, $numColumn, 'STT', $header);
        $worksheet1->write($numRow, ++$numColumn, $name_trans_ID, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_secondary_agent_id, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_username, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_amount, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_card_type."/\r\n".$name_card_pin.'/'.$name_card_serial, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_date, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_status, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_note, $header);
        
        //data
        $stt = 1;
        foreach($exportData['result']['result'] as $row){
            $numRow++;
            $numColumn=0;
            $stt++;
            $worksheet1->write($numRow, $numColumn, $stt, $numberFormat2);
            $worksheet1->writeString($numRow, ++$numColumn, (isset($row['transaction_id'])) ? $row['transaction_id'] : '-', $textStyle);
            $worksheet1->writeString($numRow, ++$numColumn, (isset($row['secondary_agent_id'])) ? $row['secondary_agent_id'] : '-', $textStyle);
            $worksheet1->writeString($numRow, ++$numColumn, (isset($row['username'])) ? $row['username'] : '-', $textStyle);
            if(isset($row['amount']) && $row['amount']){
                $worksheet1->write($numRow, ++$numColumn, $row['amount'], $numberFormat);
            }else{
                $worksheet1->write($numRow, ++$numColumn, 0, $numberFormat2);
            }
            $worksheet1->write($numRow, ++$numColumn, ((isset($row['card_type'])) ? $row['card_type'] : '-')."\r\n/".((isset($row['card_pin'])) ? $row['card_pin'] : '-')."\r\n/".((isset($row['card_serial'])) ? $row['card_serial'] : '-'), $textStyle);
            $worksheet1->write($numRow, ++$numColumn, (isset($row['update_date'])) ? $row['update_date'] : '-', $textStyle);
            $worksheet1->write($numRow, ++$numColumn, (isset($row['status']) && $row['status'] == 1) ? 'Thành công' : 'Thất bại', $textStyle);
            $worksheet1->write($numRow, ++$numColumn, ((isset($row['status']) && $row['status'] == 0)) ? 'Người dùng tự hủy' : (((isset($row['status']) && $row['status'] == -1) ? 'Thẻ không hợp lệ...' : 'Không xác định được lỗi')), $textStyle);
            ++$stt; 
        }
        
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $workbook->send('CardTransaction_'.$exportData['gameId'].'_'.$exportData['dateStart'].".xls");
        else
		  $workbook->send('CardTransaction_'.$exportData['gameId'].'_'.$exportData['dateStart'].'_'.$exportData['dateEnd'].".xls");
		$workbook->close();
    }
    public function exportExchangeConvert($exportData){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            die('Có lỗi trong quá trình xuất file excel. Vui lòng thử lại sau hoặc thông báo cho technical để khắc phục sự cố!');
        }
        
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet($exportData['index']);
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,6);
        $worksheet1->setColumn(1,3,12);
        $worksheet1->setColumn(4,4,20);
        $worksheet1->setColumn(5,30,12);
        
		$title = &$workbook->addFormat();
		$title->setBold();
        $title->setSize(12);
		$title->setColor('white');
		$title->setFgColor('grey');
		$title->setHAlign('center');
		$title->setVAlign('center');
		$title->setBorder(1);	
        $title->setTextWrap();
        
		//end style	
		$header = &$workbook->addFormat();
		$header->setBold();
		$header->setColor('white');
		$header->setFgColor('grey');
		$header->setHAlign('center');
		$header->setVAlign('center');
		$header->setBorder(1);	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat();
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('left');
		$textStyle->setVAlign('center');
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setHAlign('right');
		$numberFormat->setVAlign('center');
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setHAlign('right');
		$numberFormat2->setVAlign('center');
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        
        //title
        $numRow = 0;
        $numColumn = 0;
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $worksheet1->write($numRow, $numColumn, 'Exchange Convert '.$exportData['gameId'].' ( '.$exportData['dateStart'].' )', $header);
        else
            $worksheet1->write($numRow, $numColumn, 'Exchange Convert '.$exportData['gameId'].' ( '.$exportData['dateStart'].' - '.$exportData['dateEnd'].' )', $header);
        $worksheet1->setMerge(0,0,0,4);
        
        // header        
        $numRow+=2;
        $numColumn = 0;
        
        $worksheet1->write($numRow, $numColumn, 'STT', $header);
        $worksheet1->write($numRow, ++$numColumn, 'Username', $header);
        $worksheet1->write($numRow, ++$numColumn, 'Gold', $header);
        $worksheet1->write($numRow, ++$numColumn, 'Tiền convert', $header);
        $worksheet1->write($numRow, ++$numColumn, 'Server', $header);
        
        //data
        $stt = 1;
        foreach($exportData['result']['result'] as $row){
            $numRow++;
            $numColumn=0;
            $worksheet1->write($numRow, $numColumn, $stt++, $numberFormat2);
            $worksheet1->write($numRow, ++$numColumn, (isset($row['username'])) ? $row['username'] : '-', $textStyle);
            if(isset($row['total_knb']) && $row['total_knb']){
                $worksheet1->write($numRow, ++$numColumn, $row['total_knb'], $numberFormat);
            }else{
                $worksheet1->write($numRow, ++$numColumn, 0, $numberFormat2);
            }
            if(isset($row['total_gold']) && $row['total_gold']){
                $worksheet1->write($numRow, ++$numColumn, $row['total_gold'], $numberFormat);
            }else{
                $worksheet1->write($numRow, ++$numColumn, 0, $numberFormat2);
            }
            $worksheet1->write($numRow, ++$numColumn, (isset($row['server_id']) && isset($exportData['configChannelGame']->server->$row['server_id']->name)) ? $exportData['configChannelGame']->server->$row['server_id']->name : (($row['server_id']) ? '['.$row['server_id'].']' : '-'), $textStyle);
        }
        
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $workbook->send('ExchangeConvert_'.$exportData['gameId'].'_'.$exportData['dateStart'].".xls");
        else
		  $workbook->send('ExchangeConvert_'.$exportData['gameId'].'_'.$exportData['dateStart'].'_'.$exportData['dateEnd'].".xls");
		$workbook->close();
    }
    public function exportExchangeConvertKPI($exportData){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            die('Có lỗi trong quá trình xuất file excel. Vui lòng thử lại sau hoặc thông báo cho technical để khắc phục sự cố!');
        }
        
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet($exportData['index']);
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,12);
        $worksheet1->setColumn(1,30,12);
        
		$title = &$workbook->addFormat();
		$title->setBold();
        $title->setSize(12);
		$title->setColor('white');
		$title->setFgColor('grey');
		$title->setHAlign('center');
		$title->setVAlign('center');
		$title->setBorder(1);	
        $title->setTextWrap();
        
		//end style	
		$header = &$workbook->addFormat();
		$header->setBold();
		$header->setColor('white');
		$header->setFgColor('grey');
		$header->setHAlign('center');
		$header->setVAlign('center');
		$header->setBorder(1);	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat();
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('left');
		$textStyle->setVAlign('center');
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setHAlign('right');
		$numberFormat->setVAlign('center');
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setHAlign('right');
		$numberFormat2->setVAlign('center');
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        
        //title
        $numRow = 0;
        $numColumn = 0;
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $worksheet1->write($numRow, $numColumn, 'Exchange Convert KPI '.$exportData['gameId'].' ( '.$exportData['dateStart'].' )', $header);
        else
            $worksheet1->write($numRow, $numColumn, 'Exchange Convert KPI '.$exportData['gameId'].' ( '.$exportData['dateStart'].' - '.$exportData['dateEnd'].' )', $header);
        $worksheet1->setMerge(0,0,0,4);
        
        // header        
        $numRow+=2;
        $numColumn = 0;
        $worksheet1->write($numRow, $numColumn, 'Channel', $header);
        $worksheet1->write($numRow, ++$numColumn, 'Server', $header);
        foreach($exportData['periodGold'] as $row){
            $arrTemp = explode(',',$row);
            $worksheet1->write($numRow, ++$numColumn, $arrTemp[0].' - '.$arrTemp[1], $header);
        }
        //data
        $numRow++;
        $numColumn=0;
        $worksheet1->write($numRow, $numColumn, (isset($exportData['channelId']) && isset($exportData['configChannelGame']->channel->{$exportData['channelId']}->name)) ? $exportData['configChannelGame']->channel->{$exportData['channelId']}->name : (isset($exportData['channelId']) ? $exportData['channelId'] : '-'), $textStyle);
        $worksheet1->write($numRow, ++$numColumn, (isset($exportData['serverId']) && isset($exportData['configChannelGame']->server->{$exportData['serverId']}->name)) ? $exportData['configChannelGame']->server->{$exportData['serverId']}->name : (isset($exportData['serverId']) ? $exportData['serverId'] : '-'), $textStyle);
        foreach($exportData['periodGold'] as $row){
            if(isset($exportData['result']['result'][$row]) && $exportData['result']['result'][$row]){
                $worksheet1->write($numRow, ++$numColumn, $exportData['result']['result'][$row], $numberFormat);
            }else{
                $worksheet1->write($numRow, ++$numColumn, 0, $numberFormat2);
            }
        }
        
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $workbook->send('ExchangeConvertKPI_'.$exportData['gameId'].'_'.$exportData['dateStart'].".xls");
        else
		  $workbook->send('ExchangeConvertKPI_'.$exportData['gameId'].'_'.$exportData['dateStart'].'_'.$exportData['dateEnd'].".xls");
		$workbook->close();
    }
    public function exportTransaction($exportData){
        if(!is_array($exportData) && (is_array($exportData) || count($exportData) == 0)){
            die('Có lỗi trong quá trình xuất file excel. Vui lòng thử lại sau hoặc thông báo cho technical để khắc phục sự cố!');
        }
        
        $workbook = My_Pear::getInstanceSpreadsheet();
	    $workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet($exportData['index']);
        $worksheet1->setInputEncoding('utf-8');
        $worksheet1->setColumn(0,0,12);
        $worksheet1->setColumn(1,30,12);
        
		$title = &$workbook->addFormat();
		$title->setBold();
        $title->setSize(12);
		$title->setColor('white');
		$title->setFgColor('grey');
		$title->setHAlign('center');
		$title->setVAlign('center');
		$title->setBorder(1);	
        $title->setTextWrap();
        
		//end style	
		$header = &$workbook->addFormat();
		$header->setBold();
		$header->setColor('white');
		$header->setFgColor('grey');
		$header->setHAlign('center');
		$header->setVAlign('center');
		$header->setBorder(1);	
        $header->setTextWrap();

		$textStyle = &$workbook->addFormat();
		$textStyle->setAlign('vcentre');
		$textStyle->setHAlign('left');
		$textStyle->setVAlign('center');
        $textStyle->setBorder(1);
        $textStyle->setTextWrap();
		
		$numberFormat = &$workbook->addFormat();
		$numberFormat->setHAlign('right');
		$numberFormat->setVAlign('center');
		$numberFormat->setNumFormat('#,###');
        $numberFormat->setBorder(1);
        $numberFormat->setTextWrap();
		
		$numberFormat2 = &$workbook->addFormat();
		$numberFormat2->setAlign('vcentre');
		$numberFormat2->setHAlign('right');
		$numberFormat2->setVAlign('center');
        $numberFormat2->setBorder(1);
        $numberFormat2->setTextWrap();
        
        //title
        $numRow = 0;
        $numColumn = 0;
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $worksheet1->write($numRow, $numColumn, 'Transaction '.$exportData['gameId'].' ( '.$exportData['dateStart'].' )', $header);
        else
            $worksheet1->write($numRow, $numColumn, 'Transaction '.$exportData['gameId'].' ( '.$exportData['dateStart'].' - '.$exportData['dateEnd'].' )', $header);
        $worksheet1->setMerge(0,0,0,4);
        
        // header        
        $numRow+=2;
        $numColumn = 0;
        
        $name_trans_ID = 'Transaction ID';
        if(isset($exportData['configAPIGame']->transaction->allow->view->trans_ID)){
            $name_trans_ID = $exportData['configAPIGame']->transaction->allow->view->trans_ID;
        }
        $name_trans_Partner_ID = 'Partner Transaction ID';
        if(isset($exportData['configAPIGame']->transaction->allow->view->trans_Partner_ID)){
            $name_trans_Partner_ID = $exportData['configAPIGame']->transaction->allow->view->trans_Partner_ID;
        }
        $name_username = 'Username';
        if(isset($exportData['configAPIGame']->transaction->allow->view->username)){
            $name_username = $exportData['configAPIGame']->transaction->allow->view->username;
        }
        $name_gold = 'Tiền trong game';
        if(isset($exportData['configAPIGame']->transaction->allow->view->gold)){
            $name_gold = $exportData['configAPIGame']->transaction->allow->view->gold;
        }
        $name_gold_convert = 'Tiền convert';
        if(isset($exportData['configAPIGame']->transaction->allow->view->gold_convert)){
            $name_gold_convert = $exportData['configAPIGame']->transaction->allow->view->gold_convert;
        }
        $name_date = 'Ngày';
        if(isset($exportData['configAPIGame']->transaction->allow->view->date)){
            $name_date = $exportData['configAPIGame']->transaction->allow->view->date;
        }
        $name_server = 'Server';
        if(isset($exportData['configAPIGame']->transaction->allow->view->server)){
            $name_server = $exportData['configAPIGame']->transaction->allow->view->server;
        }
        $name_channel = 'Channel';
        if(isset($exportData['configAPIGame']->transaction->allow->view->channel)){
            $name_channel = $exportData['configAPIGame']->transaction->allow->view->channel;
        }
        $name_channel_secondary = 'Channel Secondary';
        if(isset($exportData['configAPIGame']->transaction->allow->view->channel_secondary)){
            $name_channel_secondary = $exportData['configAPIGame']->transaction->allow->view->channel_secondary;
        }
        $name_status = 'Trạng thái';
        if(isset($exportData['configAPIGame']->transaction->allow->view->status)){
            $name_status = $exportData['configAPIGame']->transaction->allow->view->status;
        }
        $name_note = 'Note';
        if(isset($exportData['configAPIGame']->transaction->allow->view->note)){
            $name_note = $exportData['configAPIGame']->transaction->allow->view->note;
        }
        $worksheet1->write($numRow, $numColumn, 'STT', $header);
        if(isset($exportData['configAPIGame']->transaction->tranPartner) && $exportData['configAPIGame']->transaction->tranPartner == 1)
            $worksheet1->write($numRow, ++$numColumn, $name_trans_ID, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_trans_Partner_ID, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_username, $header);
        if(isset($exportData['configAPIGame']->transaction->gold) && $exportData['configAPIGame']->transaction->gold == 1)
            $worksheet1->write($numRow, ++$numColumn, $name_gold, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_gold_convert, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_date, $header);
        if(!isset($exportData['configAPIGame']->transaction->server) || (isset($exportData['configAPIGame']->transaction->server) && $exportData['configAPIGame']->transaction->server == 1))
            $worksheet1->write($numRow, ++$numColumn, $name_server, $header);
        if(!isset($exportData['configAPIGame']->transaction->agent_id) || (isset($exportData['configAPIGame']->transaction->agent_id) && $exportData['configAPIGame']->transaction->agent_id == 1))
            $worksheet1->write($numRow, ++$numColumn, $name_channel, $header);
        if(!isset($exportData['configAPIGame']->transaction->secondary_agent_id) || (isset($exportData['configAPIGame']->transaction->secondary_agent_id) && $exportData['configAPIGame']->transaction->secondary_agent_id == 1))
            $worksheet1->write($numRow, ++$numColumn, $name_channel_secondary, $header);
        $worksheet1->write($numRow, ++$numColumn, $name_status, $header);
        if(isset($exportData['configAPIGame']->transaction->fail) && $exportData['configAPIGame']->transaction->fail)
            $worksheet1->write($numRow, ++$numColumn, $name_note, $header);
        
        //data
        $stt = 1;
        foreach($exportData['result']['result'] as $row){
            $numRow++;
            $numColumn=0;
            $worksheet1->write($numRow, $numColumn, $stt++, $numberFormat2);
            $worksheet1->writeString($numRow, ++$numColumn, (isset($row['transaction_id'])) ? $row['transaction_id'] : '-', $textStyle);
            if(isset($exportData['configAPIGame']->transaction->tranPartner) && $exportData['configAPIGame']->transaction->tranPartner == 1)
                $worksheet1->writeString($numRow, ++$numColumn, (isset($row['agent_transaction_id'])) ? $row['agent_transaction_id'] : '-', $textStyle);
            $worksheet1->writeString($numRow, ++$numColumn, (isset($row['username'])) ? $row['username'] : '-', $numberFormat2);
            if(isset($row['knb']) && $row['knb']){
                $worksheet1->write($numRow, ++$numColumn, $row['knb'], $numberFormat);
            }else{
                $worksheet1->write($numRow, ++$numColumn, 0, $numberFormat2);
            }
            if(isset($exportData['configAPIGame']->transaction->gold) && $exportData['configAPIGame']->transaction->gold == 1){
                if(isset($row['gold']) && $row['gold']){
                    $worksheet1->write($numRow, ++$numColumn, $row['gold'], $numberFormat);
                }else{
                    $worksheet1->write($numRow, ++$numColumn, 0, $numberFormat2);
                }
            }
            $worksheet1->write($numRow, ++$numColumn, (isset($row['update_date'])) ? date("Y-m-d H:i:s",strtotime($row['update_date'])) : '-', $textStyle);
            if(!isset($exportData['configAPIGame']->transaction->server) || (isset($exportData['configAPIGame']->transaction->server) && $exportData['configAPIGame']->transaction->server == 1)){
				$worksheet1->writeString($numRow, ++$numColumn, (isset($row['server_id']) && isset($exportData['configChannelGame']->server->$row['server_id']->name)) ? $exportData['configChannelGame']->server->$row['server_id']->name : (isset($row['server_id']) ? $row['server_id'] : '-'), $textStyle);
            }
            if(!isset($exportData['configAPIGame']->transaction->agent_id) || (isset($exportData['configAPIGame']->transaction->agent_id) && $exportData['configAPIGame']->transaction->agent_id == 1)){
                $worksheet1->writeString($numRow, ++$numColumn, (isset($row['agent_id']) && isset($exportData['configChannelGame']->channel->$row['agent_id']->name)) ? $exportData['configChannelGame']->channel->$row['agent_id']->name : (isset($row['agent_id']) ? $row['agent_id'] : '-'), $textStyle);
            }
            if(!isset($exportData['configAPIGame']->transaction->secondary_agent_id) || (isset($exportData['configAPIGame']->transaction->secondary_agent_id) && $exportData['configAPIGame']->transaction->secondary_agent_id == 1)){
                $worksheet1->writeString($numRow, ++$numColumn, (isset($row['secondary_agent_id'])) ? $row['secondary_agent_id'] : '-', $textStyle);
            }
//            $status = '';
//            if(isset($row['statusAll'])){
//                if($row['statusAll'] == 1){
//                    $totalKNB += $row['knb'];
//                    $totalGold += $row['gold'];
//                    $totalTransactionSuccess += 1;
//                    $status = 'Thành công';
//                }else{
//                    if(isset($exportData['configAPIGame']->transaction->success)){
//                        $success = ','.$exportData['configAPIGame']->transaction->success.',';
//                    }else{
//                        $success = ',1,';
//                    }
//                    $checkStatus = strrpos('checkStatus_'.$row['statusAll'],$success);
//                    
//                    if($checkStatus > 1){
//                        $totalKNB += $row['knb'];
//                        $totalGold += $row['gold'];
//                        $totalTransactionSuccess += 1;
//                        $status = 'Thành công';
//                    }else{
//                        $status = 'Thất bại';
//                    }
//                }
//            }else{
//                if($row['status'] == 1){
//                    $totalKNB += $row['knb'];
//                    $totalGold += $row['gold'];
//                    $totalTransactionSuccess += 1;
//                    $status = 'Thành công';
//                }else{
//                    $status = 'Thất bại';
//                }
//            }
//            $worksheet1->writeString($numRow, ++$numColumn, $status, $textStyle);
            //if(isset($exportData['configAPIGame']->transaction->fail) && $exportData['configAPIGame']->transaction->fail){
//                $statusAll ='';
//                if(isset($exportData['configAPIGame']->transaction->cancel) && $exportData['configAPIGame']->transaction->cancel == $row['status']){
//                    $statusAll = 'Hủy giao dịch';
//                }elseif($status == 'Thất bại'){
//                    $arrStatusFail = $exportData['configAPIGame']->transaction->fail->toArray();
//                    foreach($arrStatusFail as $fail){
//                        if(strpos('start'.$row['statusAll'],',')){
//                            $check = strrpos('start'.$row['statusAll'],','.$fail['value'].',');
//                        }else{
//                            $check = strrpos('start'.$row['statusAll'],$fail['value']);
//                        }
//                        if($fail['cond'] == "=="){
//                            if($check){
//                                $statusAll = $fail['mess'];
//                            }
//                        }else{
//                            if(isset($fail['sub']) && isset($fail['sub']['cond']) && isset($fail['sub']['value'])){
//                                $check2 = strrpos('start'.$row['statusAll'],','.$fail['sub']['value'].',');
//                                if($fail['sub']['cond'] == "=="){
//                                    if($check2){
//                                        if(!$check){
//                                            $statusAll = $fail['mess'];
//                                        }
//                                    }
//                                }
//                            }elseif(!$check){
//                                $statusAll = $fail['mess'];
//                            }
//                        }
//                    }
//                }
//                $worksheet1->writeString($numRow, ++$numColumn, $statusAll, $textStyle);
//            }
        }
        
        if($exportData['dateStart'] == $exportData['dateEnd'])
            $workbook->send('Transaction_'.$exportData['gameId'].'_'.$exportData['dateStart'].".xls");
        else
		  $workbook->send('Transaction_'.$exportData['gameId'].'_'.$exportData['dateStart'].'_'.$exportData['dateEnd'].".xls");
		$workbook->close();
    }
}